package com.thekernel.youdaoyun.controller;

import com.alibaba.fastjson.JSONObject;
import com.thekernel.youdaoyun.bean.Label;
import com.thekernel.youdaoyun.bean.Note;
import com.thekernel.youdaoyun.exception.MyException;
import com.thekernel.youdaoyun.exception.impl.DataAlreadyExistsException;
import com.thekernel.youdaoyun.exception.impl.DataNotFoundException;
import com.thekernel.youdaoyun.exception.impl.SystemErrorException;
import com.thekernel.youdaoyun.service.LabelService;
import com.thekernel.youdaoyun.util.ResponseUtils;
import com.thekernel.youdaoyun.util.UserUtils;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @Author: thekernel
 * @Date: 2020/3/26 16:51
 * @Version: 1.0
 * @Description:
 */
@RestController
@RequestMapping("/label")
public class LabelController {

    @Autowired
    private LabelService labelService;


    /**
     * 添加标签 API
     *
     * @param data 标签名等
     * @return String 是否成功以及信息
     */
//    @Log("label_add")
    @ApiOperation(value = "添加标签", notes = "用户添加标签")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "label", value = "标签名", required = true, dataType = "string"),
    })
    @PostMapping("add")
    public @ResponseBody
    String addLabel(@RequestBody JSONObject data) throws MyException {
        Label label = data.toJavaObject(Label.class);
        label.setUserId(UserUtils.checkReturnUserId());
        int result = this.labelService.addLabel(label);

        if (result == 1) {
            JSONObject returnData = new JSONObject();
            returnData.put("labelId", label.getId());
            return ResponseUtils.response(ResponseUtils.SUCCESS, "Label Add Success", returnData);
        } else if (result == 0) {
            throw new DataAlreadyExistsException();
        } else if (result == -1) {
            throw new DataNotFoundException();
        } else {
            throw new SystemErrorException();
        }
    }

    /**
     * 用户查看所有标签
     *
     * @return 标签列表
     * @throws MyException 非本人操作等
     */
//    @Log("query_all_labels")
    @ApiOperation(value = "查询所有标签", notes = "根据用户登录信息获取用户已有所有标签")
    @GetMapping("labels")
    public @ResponseBody
    String queryAllLabels() throws MyException {
        long userId = UserUtils.checkReturnUserId();
        JSONObject returnData = new JSONObject();
        List<Label> labels = Arrays.asList(this.labelService.queryLabels(userId));
        returnData.put("data", labels);
        returnData.put("num", labels.size());
        return ResponseUtils.response(ResponseUtils.SUCCESS, "Query Labels Success", returnData);
    }

    /**
     * 根据笔记查看标签
     *
     * @return 标签列表
     * @throws MyException 非本人操作等
     */
//    @Log("query_note_labels")
    @ApiOperation(value = "查询笔记所属所有标签", notes = "根据笔记查询标签")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "noteId", value = "笔记id", required = true, dataType = "long")
    })
    @GetMapping("note/labels")
    public @ResponseBody
    String queryNoteLabels(@RequestBody JSONObject data) throws MyException {
        int noteId = (int) data.get("noteId");
        JSONObject returnData = new JSONObject();
        List<Label> labels = Arrays.asList(this.labelService.queryNoteLabels(noteId));
        returnData.put("data", labels);
        returnData.put("num", labels.size());
        return ResponseUtils.response(ResponseUtils.SUCCESS, "Query Labels Success", returnData);
    }

    /**
     * 根据标签查询笔记
     *
     * @return 笔记列表
     * @throws MyException 非本人操作等
     */
//    @Log("query_note_by_label")
    @ApiOperation(value = "查询标签下所有笔记", notes = "根据标签查询笔记")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "labelId", value = "标签id", required = true, dataType = "long")
    })
    @GetMapping("notes")
    public @ResponseBody
    String queryNoteByLabel(@RequestBody JSONObject data) throws MyException {
        int labelId = (int) data.get("labelId");
        JSONObject returnData = new JSONObject();
        List<Note> notes = Arrays.asList(this.labelService.queryNotesByLabelId(labelId));
        returnData.put("data", notes);
        returnData.put("num", notes.size());
        return ResponseUtils.response(ResponseUtils.SUCCESS, "Query Notes Success", returnData);
    }
}
